package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import android.util.SparseArray
import androidx.core.util.isEmpty
import com.lz.library.proguard.IProguardField
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

//考试计划
class ExamPlan() : ParcelableString, IProguardField {

    //计划ID, 用于表外键
    @Expose
    var id: Int = 0

    //名称
    @Expose
    var planName: String = ""

    //考试计划编号
    @Expose
    var planCode: String = ""

    //人证阈值
    @Expose
    var personThreshold: Float = 0f

    //人脸阈值
    @Expose
    var faceThreshold: Float = 0f

    //指纹阈值
    @Expose
    var fingerprintThreshold: Float = 0f

    //开始时间
    @Expose
    var planStartTime: Long = 0

    //结束时间
    @Expose
    var planEndTime: Long = 0

    //考试检验类型
    @Expose
    var examCheckType: String = ""

    //计划类型
    @Expose
    var planType = 0

    //1启动 0关闭
    @Expose
    var status: Int = 0

    //项目列表
    @Expose
    @SerializedName("busExamProjectDTOList")
    var examProjectList: List<ExamProject>? = null

    @Expose
    @SerializedName("busExamAreaDTOList")
    var examAreaList: List<ExamArea>? = null

    @Expose(serialize = false, deserialize = false)
    val examProjectMap = SparseArray<ExamProject>()

    //项目列表不为空
    fun isNotEmpty(): Boolean {
        val res = examProjectList != null && examProjectList!!.isNotEmpty()
        if (res && examProjectMap.isEmpty()) {
            examProjectList?.forEach { examProjectMap[it.id] = it }
        }
        return res
    }

    //通过ID查找项目
    fun examProject(projectId: Int): ExamProject? = examProjectMap[projectId]

    //第一个项目
    fun firstProject(): ExamProject = examProjectList!![0]

    fun examArea(areaId: Int): ExamArea? {
        for (examArea in examAreaList!!) {
            if (examArea.id == areaId) {
                return examArea
            }
        }
        return null
    }

    override fun str(): String = planCode

    override fun equals(other: Any?): Boolean = other === this || (other is ExamPlan && other.id == id)

    @Suppress("DEPRECATION")
    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        planName = parcel.readString()!!
        planCode = parcel.readString()!!
        personThreshold = parcel.readFloat()
        faceThreshold = parcel.readFloat()
        fingerprintThreshold = parcel.readFloat()
        planStartTime = parcel.readLong()
        planEndTime = parcel.readLong()
        examCheckType = parcel.readString()!!
        planType = parcel.readInt()
        status = parcel.readInt()
        examProjectList = parcel.createTypedArrayList(ExamProject)
        examAreaList = parcel.createTypedArrayList(ExamArea)
        parcel.readSparseArray<ExamProject>(javaClass.classLoader)
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeString(planName)
        parcel.writeString(planCode)
        parcel.writeFloat(personThreshold)
        parcel.writeFloat(faceThreshold)
        parcel.writeFloat(fingerprintThreshold)
        parcel.writeLong(planStartTime)
        parcel.writeLong(planEndTime)
        parcel.writeString(examCheckType)
        parcel.writeInt(planType)
        parcel.writeInt(status)
        parcel.writeTypedList(examProjectList)
        parcel.writeTypedList(examAreaList)
        parcel.writeSparseArray(examProjectMap)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ExamPlan> {
        override fun createFromParcel(parcel: Parcel): ExamPlan {
            return ExamPlan(parcel)
        }

        override fun newArray(size: Int): Array<ExamPlan?> {
            return arrayOfNulls(size)
        }
    }
}